<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Session;
class AdminController extends Controller
{

    public function showWelcome(){
        return view('admin.showWelcome');
    }
    //定义一个方法，用于显示添加会员页面
    public function showAddAdmin(){
        return view('admin.showAddAdmin');
    }
    //定义一个方法，用于处理添加会员
    public function checkAddAdmin(Request $request){
        $this->validate($request,['username'=>'required|between:6,20|unique:table_admin,username',
            'password'=>'required|between:6,20|confirmed',
        ]);

        $data['username']=$request->input('username');
        $password=$request->input('password');
        $data['password']=$data['password']=Hash::make($password);
        $result=DB::table('table_admin')->insert($data);
        if($result){
            return redirect()->back()->with('status','哈哈...恭喜小主！添加成功！！！');
        }else{
            return redirect()->back()->with('status','呜呜...抱歉小主！添加失败！！！');
        }
        //dd($data);exit;
    }
    //定义一个方法，用于显示会员列表
    public function adminList(Request $request){
        $listArr=DB::table('table_admin')->orderBy('is_super_vip','desc')->paginate(6);
        //dd($listArr);exit;
        return view('admin.adminList',['listArr'=>$listArr]);
    }

    //写一个方法，用于恢复/禁用管理员
    public function updateAdmin(Request $request){
        $updateAdmin=DB::table('table_admin')
                    ->where('id','=',"$request->admin_id")
                    ->update(['status'=>$request->status])
        ;
        $backTips=['msg'=>200];
        if($updateAdmin){
            return json_encode($backTips);
        }
    }
    //定义一个方法，用于删除会员
    public function deleteAdmin(Request $request){
        $deleteAdmin=DB::table('table_admin')->where('id', '=',"$request->admin_id")->delete();
        $backTips=['msg'=>200];
        if($deleteAdmin){
            return json_encode($backTips);
        }

    }
}
